package collection.mymap.hash;

import com.google.common.base.Objects;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import generalobjects.Animal;
import generalobjects.People;
import java.util.stream.Collectors;
import org.testng.annotations.Test;

public class MapBase2 {



    @Test
    public void changeMapInAnotherMethod() {
        Map<Integer, Integer> map = new HashMap<>();
        map.put(1, 1);
        map.put(2, 1);

        removeOneElement(map, 2);

        System.out.println(map);

    }


    @Test
    public void changeMapInAnotherMethod2() {
        Map<Integer, Integer> map = new HashMap<>();
        map.put(1, 1);
        map.put(2, 1);

        changeMapByNewMap(map, 2);

        System.out.println(map);

    }

    @Test
    public void changeMapInAnotherMethod3() {
        Map<Integer, Integer> map = new HashMap<>();
        map.put(1, 1);
        map.put(2, 1);

        changeMapByNewMap2(map, 2);

        System.out.println(map);

    }

    /**
     * clear方法清除map的值
     */
    @Test
    public void t1() {
        HashMap<Integer, Integer> map = new HashMap(6);

        map.put(1, 1);
        map.put(2, 2);
        System.out.println(map);

        map.values().clear();
//        map.clear();
        System.out.println(map);
    }

    /**
     * setValue清除map的值
     */
    @Test
    public void t2() {
        HashMap<Integer, Integer> map = new HashMap(6);

        map.put(1, 1);
        map.put(2, 2);
        System.out.println(map);

        for (Entry<Integer, Integer> entry : map.entrySet()) {
            entry.setValue(null);
        }
        // out {1=null, 2=null}
        System.out.println(map);
    }

    /**
     * 重写HashCode方法后
     * 修改get方法得到的val对象会改变原始map数据
     * @param
     * @param
     */
    @Test
    public void t3() {
        Map<String, People> map = new HashMap<>(5);
        map.put("a", new People("Timo", 23));
        System.out.println("原始map" + map);
        People people = map.get("a");
        people.setAge(1);
        System.out.println("修改后map" + map);


    }

    /**
     *
     * @param
     * @param
     */
    @Test
    public void t4() {
        Animal animal = new Animal("dog", 2);
        System.out.println(animal);

        animal.setName("cat");
        // Animal类重写hashcode方法会使得hashCode码改变，不重写的话输出相同的哈希码
        System.out.println(animal);
    }

    /**
     *
     */
    @Test
    public void t5() {
        Map<Integer, Integer> map = new HashMap<>();
        addOneElement(map, 1);
        // out {1=1}
        System.out.println(map);
    }

    @Test
    public void t6() {
        var map = new HashMap<String, Integer>();

//        var res = rewardGrid.entrySet().stream().collect(
//            Collectors.collectingAndThen(
//                Collectors.groupingBy(entry -> getTreasureSearchReward(searchRewardConfig, entry.getKey()).type),
//                map -> map.entrySet().stream()
//                    .map(entry -> {
//                        var isGetCurrentQualityAllReward = entry.getValue().stream()
//                            .allMatch(i -> i.getValue() == getTreasureSearchReward(searchRewardConfig, i.getKey()).num);
//
//                        var innerMap = new HashMap<Integer, Boolean>();
//                        innerMap.put(entry.getKey(), isGetCurrentQualityAllReward);
//                        return innerMap;
//                    })
//                    .map(HashMap::entrySet)
//                    .flatMap(Collection::stream)
//                    .filter(Entry::getValue)
//                    .map(Entry::getKey)
//                    .map(QualityTypeEnum::getByCode)
//                    .collect(Collectors.toSet())
//            )
//        );

//        map.entrySet().stream()
//            .map(entry -> new HashMap<>())
            // 只有entry才能调用getKey，map不能直接调用getKey
//            .collect(Collectors.toMap(Entry::getKey, Entry::getValue));

    }

    /**
     *
     * @param map
     * @param i
     */
    public void removeOneElement(Map<Integer, Integer> map, Integer i) {
        map.remove(i);
    }

    public void addOneElement(Map<Integer, Integer> map, Integer i) {
        map.put(i, i);
    }

    public void changeMapByNewMap(Map<Integer, Integer> map, Integer i) {
        Map<Integer, Integer> map1 = new HashMap<>(map);
        map1.remove(i);

        map = map1;
    }

    public void changeMapByNewMap2(Map<Integer, Integer> map, Integer i) {
        Map<Integer, Integer> map1 = new HashMap<>(map);
        System.out.println("map from changeMapByNewMap2 after" + map1);
        map1.remove(i);

        map.putAll(map1);
    }

    public Map<Integer, Integer> changeMapByNewMap3(Map<Integer, Integer> map, Integer i) {
        Map<Integer, Integer> map1 = new HashMap<>(map);
        map1.remove(i);


        map = map1;
        return map;
    }




}



